package com.itboye.zhujia.me;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.zhujia.R;
import com.itboye.zhujia.base.BaseActivity;
import com.itboye.zhujia.constants.SPContants;
import com.itboye.zhujia.network.HttpRequest;
import com.itboye.zhujia.util.DebugLog;
import com.itboye.zhujia.util.SPUtils;
import com.itboye.zhujia.util.T;
import com.itboye.zhujia.volley.CodeErrorException;
import com.itboye.zhujia.volley.MyJsonRequest;
import com.itboye.zhujia.volley.XErrorListener;
import com.itboye.zhujia.volley.XRequestListener;
/**
 * 修改昵称
 *
 */
public class ActivityNiCheng extends BaseActivity implements OnClickListener{
	private ImageView close_icon;//返回图片
	private TextView add_shap_title_tv;//标题
	EditText nicheng_et;//昵称
	TextView nicheng_ok;//完成

	@Override
	public int initView() {
		return 0;
	}

	@Override
	public void onMyCreate() {
		setContentView(R.layout.activity_ni_cheng);
		
		add_shap_title_tv.setText("修改昵称");

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.nicheng_ok:
			if (nicheng_et.getText().toString().equals(null)
					&&nicheng_et.getText().toString()=="") {
				
				T.showShort(ActivityNiCheng.this, "昵称不能为空");
				
				return;
			} else {
				setNickName();
			}
			break;

		default:
			break;
		}
		
	}
	/**
	 * 修改昵称
	 */
	private void setNickName() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_User_update")
				.param("uid",
						(String) SPUtils.get(ActivityNiCheng.this, null, SPContants.USER_ID,
								""))
				.param("nickname", nicheng_et.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						DebugLog.v("result", response.toString());
						T.showShort(ActivityNiCheng.this, "修改成功");
						SPUtils.put(ActivityNiCheng.this, null, SPContants.NICK, nicheng_et
								.getText().toString());

						Intent intent = new Intent(ActivityNiCheng.this,
								ActivityMyData.class);
						intent.putExtra("nickname", nicheng_et.getText()
								.toString());
						setResult(RESULT_OK, intent);
						ActivityNiCheng.this.finish();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showShort(ActivityNiCheng.this, "网络异常");
						} else {
							T.showShort(ActivityNiCheng.this, "网络异常");
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

}
